package CutLoad.mc
{
	import CutLoad.CutLoad;
	import CutLoad.cookies.Painter;
	
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 将一组电影符号组合成一个，含有垂直滚动条，显示内容。
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 下午02:16:44
	 *
	 */	
	public class McToScrollH
	{
		private var bodyWidth:Number,bodyHeight:Number;
		private var spMask:Sprite,mask:Rectangle;
		private var mcBar:MovieClip,mcBarBack:MovieClip,body:MovieClip;
		private var isDrag:Boolean;
		private var mcBar_start_y:Number;
		private var body_start_y:Number;
		public function McToScrollH(mcBar:MovieClip,mcBarBack:MovieClip,body:MovieClip,mask:Rectangle)
		{
			mcBar.buttonMode=true;
			mcBar.x=mcBarBack.x;
			this.mask=mask;
			this.mcBarBack=mcBarBack;
			this.body=body;
			this.mcBar=mcBar;
			mcBar_start_y=mcBar.y;
			body_start_y=body.y;
			isDrag=false;
			//
			bodyWidth=body.width;
			bodyHeight=body.height;
			spMask=Painter.getAlphaRect(mask.width,mask.height);
			spMask.x=mask.x;
			spMask.y=mask.y;
			body.parent.addChild(spMask);
			body.mask=spMask;
			//如果内容小于遮蔽范围，就不需要滚动条
			if(bodyHeight<mask.height){
				mcBar.visible=false;
				mcBarBack.visible=false;
			}
			//
			mcBar.addEventListener(MouseEvent.MOUSE_DOWN,down);
			mcBar.addEventListener(Event.ENTER_FRAME,enterFrame);
			body.addEventListener(MouseEvent.MOUSE_DOWN,down_body);
			body.addEventListener(MouseEvent.MOUSE_UP,body_mouse_up);
		}
		public function killMe():void{
			if(mcBar!=null){
				if(mcBar.hasEventListener(MouseEvent.MOUSE_DOWN)) mcBar.addEventListener(MouseEvent.MOUSE_DOWN,down);
				if(mcBar.hasEventListener(Event.ENTER_FRAME)) mcBar.addEventListener(Event.ENTER_FRAME,enterFrame);
				if(mcBar.stage.hasEventListener(MouseEvent.MOUSE_WHEEL)) mcBar.stage.removeEventListener(MouseEvent.MOUSE_WHEEL,mouse_wheel);
			}
			if(body!=null){
				if(body.hasEventListener(MouseEvent.MOUSE_DOWN)) body.removeEventListener(MouseEvent.MOUSE_DOWN,down_body);
				if(body.hasEventListener(MouseEvent.MOUSE_UP))body.removeEventListener(MouseEvent.MOUSE_UP,body_mouse_up);
				body.mask=null;
				if(spMask!=null) if(spMask.parent!=null) spMask.parent.removeChild(spMask);
			}
		}
		private function down(e:MouseEvent):void{
			isDrag=true;
			mcBar.startDrag(false,new Rectangle(mcBarBack.x,mcBarBack.y,0,mcBarBack.height-mcBar.height));
			mcBar.stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}
		private function down_body(e:MouseEvent):void{
			isDrag=true;
			mcBar.stage.addEventListener(MouseEvent.MOUSE_WHEEL,mouse_wheel);
		}
		private function body_mouse_up(e:MouseEvent):void{
			if(mcBar.stage.hasEventListener(MouseEvent.MOUSE_WHEEL)) mcBar.stage.removeEventListener(MouseEvent.MOUSE_WHEEL,mouse_wheel);
		}
		private function mouseUp(e:MouseEvent):void{
			isDrag=false;
			mcBar.stopDrag();
			mcBar.stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);
			if(mcBar.stage.hasEventListener(MouseEvent.MOUSE_WHEEL)) mcBar.stage.removeEventListener(MouseEvent.MOUSE_WHEEL,mouse_wheel);
		}
		private function mouse_wheel(e:MouseEvent):void{
			if(e.delta<0){
				mcBar.y-=e.delta;
				var maxY:Number=mcBarBack.y+mcBarBack.height-mcBar.height
				if(mcBar.y>maxY) mcBar.y=maxY;
			}else{
				mcBar.y+=e.delta;
				if(mcBar.y<mcBarBack.y) mcBar.y=mcBarBack.y;
			}
		}
		/**
		 * 获得滚动条位置 
		 * @return 
		 * 
		 */		
		public function get value():Number{
			return (mcBar.y-mcBar_start_y)/(mcBarBack.height-mcBar.height);
		}
		private function enterFrame(e:Event):void{
			if(!isDrag) return;
			TweenLite.to(body,1,{y:-(body.height-mask.height)*value+mcBar_start_y});
		}
	}
}